function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

$(function () {
	
    
    
	var orderId = getQueryString("orderId");
	if(!orderId) return;
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/container/list?orderId=' + orderId,
        datatype: "json",
        colModel: [			
			{ label: '系统编号', name: 'number', index: 'number', width: 50, key: true },
			{ label: '集装箱号', name: 'containerno', index: 'containerno', width: 80 }, 			
			{ label: '箱型', name: 'type', index: 'type', width: 80 }, 			
			{ label: '件数', name: 'packages', index: 'packages', width: 80 }, 	
			{ label: '量重', name: 'weight', index: 'weight', width: 80 }, 	
			{ label: '寸尺', name: 'measurement', index: 'measurement', width: 80 }, 			
			{ label: '封号', name: 'closeno', index: 'closeno', width: 80 }, 			
			{ label: '司机', name: 'driver', index: 'driver', width: 80 }, 			
			{ label: '车辆', name: 'car', index: 'car', width: 80 }	
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		act: 'list',
		title: null,
		ordertracking: {},
		container: {},
		orderId: null,
		time: ''
	},
	created:function(){
		this.orderId = getQueryString('orderId');
		this.waybill = getQueryString('waybill');
		var act = getQueryString('act');
		this.act = act ? act : 'list';
		console.log("orderId==================" + this.orderId);
		//this.container.orderId = this.orderId;
		var id = getQueryString('id');
		this.getInfo(id);
		this.currentTime();
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			this.act = 'edit';
			vm.title = "新增";
			vm.container.number = null;
		},
		update: function (event) {
			var number = getSelectedRow();
			if(number == null){
				return ;
			}
			this.act = 'edit';
            vm.title = "修改";
            
            vm.getInfo(number)
		},
		saveOrUpdate: function (event) {
			var url = vm.container.id == null ? "sys/container/save" : "sys/container/update";
			console.log('baseURL='+baseURL);
			vm.container.dispatchTime = $("#dispatchTime").val();
			vm.container.arriveDate = $("#arriveDate").val();
			console.log(JSON.stringify(vm.container));
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.container),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							//vm.reload();
							//open(baseURL + "modules/sys/ordertracking.html?act=edit&step=2&id=" + vm.container.id);
							//location.href = baseURL + "modules/sys/ordertracking.html?act=edit&step=2&id=" + vm.container.id;
							location.href = "modules/sys/ordertracking.html?act=edit&step=2&id=" + vm.container.orderId;
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var numbers = getSelectedRows();
			if(numbers == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/container/delete",
                    contentType: "application/json",
				    data: JSON.stringify(numbers),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			console.log('getInfo id=' + id);
			if(id) {
				$.get(baseURL + "sys/container/info/"+id, function(r){
	                vm.container = r.container;
					$("#dispatchTime").val(vm.container.dispatchTime);
					$("#arriveDate").val(vm.container.arriveDate);
					
					$.get(baseURL + "sys/ordertracking/info/"+vm.container.orderId, function(o){
		                vm.ordertracking = o.ordertracking;
		            });
	            });
				

			} else {
				this.container.orderId = this.orderId;
				this.container.waybill = this.waybill;
			}
		},
		reload: function (event) {
			location.href = baseURL + "modules/sys/ordertracking.html?act=edit&step=2&id=" + vm.container.orderId;
			//vm.showList = true;
			//this.act = 'list';
			//var page = $("#jqGrid").jqGrid('getGridParam','page');
			//$("#jqGrid").jqGrid('setGridParam',{ 
            //    page:page
            //}).trigger("reloadGrid");
		},
		dateDefind: function () {
			$('#dispatchTime').datetimepicker({
		        format: 'YYYY-MM-DD HH:mm',
		        locale: moment.locale('zh-cn')
		    });	
		    $('#arriveDate').datetimepicker({
		        format: 'YYYY-MM-DD HH:mm',
		        locale: moment.locale('zh-cn')
		    });
		  //当选择器隐藏时，讲选择框只赋值给data里面的time
		    $('#dispatchTime').datetimepicker()
		      .on('hide', function (ev) {
		        var value = $("#dispatchTime").val();
		        //alert(value);
		        vm.container.dispatchTime = value;
		      });
		    $('#arriveDate').datetimepicker()
		    .on('hide', function (ev) {
		      var value = $("#arriveDate").val();
		      vm.container.arriveDate = value;
		    });
		},
	   
        currentTime: function(){
        	 var getTime=function(){
     	        var _this = vm;
     	        let yy = new Date().getFullYear();
     	        let mm = new Date().getMonth()+1;
     	        if(mm < 10) {
     	        	mm = '0' + mm;
     	        }
     	        let dd = new Date().getDate();
     	        if(dd < 10) {
     	        	dd = '0' + dd;
    	        }
     	        let hh = new Date().getHours();
     	        if(hh < 10) {
     	        	hh = '0' + hh;
    	        }
     	        let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
     	        let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
     	        _this.time = yy+'-'+mm+'-'+dd+' '+hh+':'+mf+':'+ss;
     	      }
          setInterval(getTime, 1000);
        }
	},
	mounted: function () {
	      this.dateDefind();
	    }
});